13.3 Obtaining an authorization code

The first thing you must do when calling the API is to obtain an authorization code. To do this, you submit information to the /connect/authorize endpoint of the web.oauth2 web service, which then allows you to use whatever authentication methods you have configured to identify yourself to the MyID authentication server; the authentication server then returns the authorization code.

See section 4.2.2, Requesting an authorization code for an overview of this process.

The following example uses a visible HTML form so that you can see what is going on.

Create a new HTML document called index.html.

Add the following:

Copy
<html>
<head>
    <title>Single-page PKCE client authentication through Javascript</title>
</head>
<body>
    <div id="intro">
        <p>This single-page example uses client authentication to obtain an authorization code.</p>
    </div>
</body>
</html>

After the intro div, add a div for the form:

Copy
<div id="login">
    <p>Click <b>Login</b> to authenticate to the MyID server using 
    your client credentials.</p>
    <form 
        method="post" 
        enctype="application/x-www-form-urlencoded" 
        action="https://myserver/web.oauth2/connect/authorize">
        <p>Client id: <input type="text" name="client_id" value="myid.mywebsite"></p>
        <p>Scope: <input type="text" name="scope" value="myid.rest.basic"></p>
        <p>Redirect: <input type="text" name="redirect_uri" value="http://127.0.0.1:5500/"></p>
        <p>Response Type: <input type="text" name="response_type" value="code"></p>
        <p>Code Challenge: <input type="text" name="code_challenge" value="lzKaVv4bWu06z_m0yFynJj6zttnU5gYpXah8tLYKzGg"></p>
        <p>Code Challenge Method: <input type="text" name="code_challenge_method" value="S256"></p>
        <input type="submit" value="Login">
      </form>
</div>

The form contains the following elements:

After the login div, add a script that checks for a returned authorization code, and replaces the form with a display of the code if it is available:

Copy
<script>
    // Get the authorization code from the URL parameters. 
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);
    const code = urlParams.get('code');

    // If the code is available, display it instead of the login form
    if (code) {
        document.getElementById("login").innerHTML="<p>The authorization code is: " + code;
    }
</script>

You can now open this HTML page on your local web server.

Using Visual Studio Code and the Live Server extension, right-click on the HTML file in the Explorer pane, then from the pop-up menu select Open with Live Server.

Click Login, and you are taken to the MyID Authentication web page.

You can sign in with any configured authentication methods; in the above example, the MyID server is set up for security questions or smart cards, but you may have configured your system for external identity providers, authentication codes, FIDO, Windows Hello, integrated Windows logon; any method you have configured for MyID is available.

Once you have authenticated, you are returned to the page specified in the redirect_uri parameter from the form. The web page obtains the authorization code from the URL and displays it on screen:

Obtaining an authorization code is the first step to calling the API. But before we progress further, we should make the request form a bit easier to work with.